import { Module } from '@nestjs/common'
import { AppController } from './app.controller'
import { AppService } from './app.service'
import { TypeOrmModule } from '@nestjs/typeorm'
import { RegisterModule } from './modules/api-modules/register/register.module'
import { LoginModule } from './modules/api-modules/login/login.module'
import { ConfigModule, ConfigService } from '@nestjs/config'
import mailConfig from './config/mail.config'
import dbConfig from './config/db.config'
import { APP_GUARD } from '@nestjs/core'
import { AuthGuard } from './modules/internal-modules/auth/guards/auth.guard'
import { AuthModule } from './modules/internal-modules/auth/auth.module'
import { UploadModule } from './modules/api-modules/upload/upload.module'
import { FileModule } from './modules/api-modules/file/file.module'

@Module({
  imports: [
    ConfigModule.forRoot({
      load: [mailConfig, dbConfig],
      isGlobal: true
    }),
    RegisterModule,
    LoginModule,
    UploadModule,
    FileModule,
    TypeOrmModule.forRootAsync({
      imports: [ConfigModule],
      useFactory: (configService: ConfigService) => {
        return {
          type: 'mysql',
          host: configService.get<string>('DB_HOST', 'localhost'),
          port: configService.get<number>('DB_PORT', 3306),
          username: configService.get<string>('DB_USERNAME', 'root'),
          password: configService.get<string>('DB_PASSWORD', 'mysql'),
          database: configService.get<string>('DB_DATABASE'),
          synchronize: configService.get<boolean>('DB_SYNC', true),
          autoLoadEntities: true
        }
      },
      inject: [ConfigService]
    }),
    AuthModule
  ],
  controllers: [AppController],
  providers: [
    AppService,
    // 注册全局守卫
    {
      provide: APP_GUARD,
      useClass: AuthGuard
    }
  ]
})
export class AppModule {}
